package com.se2.antiqueexhibition.Controller;


import com.se2.antiqueexhibition.Service.AdminReservationService;
import com.se2.antiqueexhibition.pojo.AdminReservation;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/reservatrion")
public class AdminReservationController {

    @Resource
    private AdminReservationService adminReservationService;

    @GetMapping("/list")
    public String list(Model model) {
        List<AdminReservation> adminReservation = adminReservationService.getAllReservations();
        model.addAttribute("adminReservation", adminReservation);
        return "news-management";
    }

    @GetMapping("/insert")
    public String insert(@ModelAttribute AdminReservation adminReservation,Model model) {
        try{
            adminReservationService.insert(adminReservation);
            model.addAttribute("adminReservation", adminReservation);
            model.addAttribute("message","预约成功");
            model.addAttribute("true", true);
            return "news-list";
        }catch(Exception e){

            model.addAttribute("message","预约失败");
            model.addAttribute("true", false);
            model.addAttribute("adminReservation", adminReservation);
            return "news-from";
        }
    }

    @PostMapping("/update")
    public String update(@ModelAttribute AdminReservation adminReservation, Model model) {
        try{
            model.addAttribute("adminReservation", adminReservation);
            model.addAttribute("message","预约修改成功");
            model.addAttribute("success", true);
            adminReservationService.update(adminReservation);
            return "redirect:list";

        }catch (Exception e) {

            model.addAttribute("message","预约修改失败");
            model.addAttribute("success", false);
            model.addAttribute("adminReservation", adminReservation);
            return "news-from";

        }
    }


}
